package com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAddDevices;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.AbsoluteSizeSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.bean.eventNotification;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAddDevices.persenter.FirstChangeAddDevicesPersenter;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAddDevices.view.ISFirstChangeAddDevicesView;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeDirectionsUse.FirstChangeDirectionsUseActvity;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import zhy.com.highlight.HighLight;
import zhy.com.highlight.position.OnTopPosCallback;
import zhy.com.highlight.shape.RectLightShape;


/**
 * 杭州惟君医疗科技有限公司15
 * Created by 20433 on 2018/3/14
 */

public class FirstChangeAddDevicesActvity extends AppCompatActivity implements ISFirstChangeAddDevicesView,View.OnClickListener {

    @Bind(R.id.activity_first_change_add_devices_iv_devics_pictures)
    ImageView activityFirstChangeAddDevicesIvDevicsPictures;
    @Bind(R.id.activity_first_change_add_devices_rl_devics_pictures)
    RelativeLayout activityFirstChangeAddDevicesRlDevicsPictures;
    @Bind(R.id.activity_first_change_add_devices_iv_cross)
    ImageView activityFirstChangeAddDevicesIvCross;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.activity_first_change_add_devices_tv_devices_name)
    TextView activityFirstChangeAddDevicesTvDevicesName;
    @Bind(R.id.activity_first_change_add_devices_tv_product_model)
    TextView activityFirstChangeAddDevicesTvProductModel;
    @Bind(R.id.activity_first_change_add_devices_tv_fetal_heart_rate_normal_range)
    TextView activityFirstChangeAddDevicesTvFetalHeartRateNormalRange;
    @Bind(R.id.activity_first_change_add_devices_tv_fetal_heart_rate_measurement_range)
    TextView activityFirstChangeAddDevicesTvFetalHeartRateMeasurementRange;
    @Bind(R.id.activity_first_change_add_devices_tv_method_use)
    TextView activityFirstChangeAddDevicesTvMethodUse;
    @Bind(R.id.activity_first_change_add_devices_bt_next_stop)
    Button activityFirstChangeAddDevicesBtNextStop;

    private PopupWindow mPopWindow;
    private PopupWindow mPopWindowBindingFailure;
    private PopupWindow mPopWindowRemoveBinding;
    private FirstChangeAddDevicesPersenter mFirstChangeAddDevicesPersenter = new FirstChangeAddDevicesPersenter(this);
    private TextView tv_title_decs;
    private EditText et_input_banding_code1;
    private TextView tv_input_banding_code_cancel;
    private TextView tv_banding_code_confirm;
    private TextView tv_desc;
    private TextView tv_error_content;
    private TextView tv_prompt;
    private TextView tv_end_binding;
    private TextView tv_rebind;
    private TextView tv_title;
    private TextView tv_prompt_desc;
    private TextView tv_i_think;
    private TextView tv_confirm_unbundling;
    private String devices_id;
    private int page_identification_code;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_change_add_devices);
        ButterKnife.bind(this);
        activityFirstChangeAddDevicesIvCross.setOnClickListener(this);
        activityFirstChangeAddDevicesBtNextStop.setOnClickListener(this);
        activityFirstChangeAddDevicesTvMethodUse.setOnClickListener(this);
        activityFirstChangeAddDevicesBtNextStop.post(new Runnable() {
            @Override
            public void run() {
                //showNextKnownTipView();
            }
        });
        Intent intent = getIntent();
        devices_id = intent.getStringExtra("devices_id");
        page_identification_code = intent.getIntExtra("Page_identification_code",0);
        mFirstChangeAddDevicesPersenter.initDate(page_identification_code);
    }
    public void showNextKnownTipView() {
        HighLight mHightLight = new HighLight(this)//
                .intercept(true)//拦截属性默认为true 使下方ClickCallback生效
                .autoRemove(true)
                .setClickCallback(new HighLight.OnClickCallback() {
                    @Override
                    public void onClick() {
                        Toast.makeText(FirstChangeAddDevicesActvity.this, "clicked and remove HightLight view by yourself", Toast.LENGTH_SHORT).show();

                    }
                })
                .addHighLight(R.id.activity_first_change_add_devices_bt_next_stop, R.layout.custom_banding_tipview, new OnTopPosCallback(), new RectLightShape());
        mHightLight.show();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public void endPage() {
        finish();
    }

    // 获取下一步的文本内容
    @Override
    public String getBtNextContent() {
        return activityFirstChangeAddDevicesBtNextStop.getText().toString().trim();
    }
    // 设置下一步的文本内容
    @Override
    public void setBtNextContent(String str) {
        activityFirstChangeAddDevicesBtNextStop.setText(str);
    }
    // 显示提示Toast信息
    @Override
    public void showToastInformation(String str) {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }
    // 跳转页面
    @Override
    public void toOhterActivity(String niname) {

    }
    // 显示进度
    @Override
    public void displayProgress() {

    }
    // 隐藏进度
    @Override
    public void hiddenProgress() {

    }
    // TODO 输入绑定码
    //输入绑定码
    @Override
    public void showInputBandingCode() {
        //设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.first_change_add_devices_input_banding_code, null);
        mPopWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindow.setContentView(contentView);
        findInputBandingCodeView(contentView);
        //显示PopupWindow
        View rootview = LayoutInflater.from(this).inflate(R.layout.activity_add_devices, null);
        mPopWindow.showAtLocation(rootview, Gravity.CENTER, 0, 0);
    }


    // 获取输入的绑定码
    @Override
    public String getInputBandingCode() {
        return et_input_banding_code1.getText().toString().trim();
    }
    //输入绑定的码 取消
    @Override
    public void cancelInputBandingCode() {
        mPopWindow.dismiss();
       // finish();
        //todo 发消息到设备也main通知已经绑定 刷新页面
        EventBus.getDefault().post(new eventNotification(page_identification_code));
    }
    //输入绑定的  确认绑定
    @Override
    public void inputBandingCodeConfirmBinding() {
        mPopWindow.dismiss();
    }
    // TODO 绑定失败
    //绑定失败
    @Override
    public void showBindingFailure() {
//设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.first_change_add_devices_banding_failure, null);
        mPopWindowBindingFailure = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindowBindingFailure.setContentView(contentView);
        findBindingFailure(contentView);
        //显示PopupWindow
        View rootview = LayoutInflater.from(this).inflate(R.layout.activity_add_devices, null);
        mPopWindowBindingFailure.showAtLocation(rootview, Gravity.CENTER, 0, 0);


    }
    //绑定失败 结束绑定
    @Override
    public void bindingFailureEndBinding() {
        mPopWindowBindingFailure.dismiss();
    }
    //绑定失败 重新绑定
    @Override
    public void bindingFailureRebind() {
        mPopWindowBindingFailure.dismiss();
    }
    // TODO 确认绑定失败
    //确认绑定失败
    @Override
    public void showRemoveBinding() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.first_change_add_devices_remove_banding, null);
        mPopWindowRemoveBinding = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindowRemoveBinding.setContentView(contentView);
        findRemoveBinding(contentView);
        //显示PopupWindow
        View rootview = LayoutInflater.from(this).inflate(R.layout.activity_add_devices, null);
        mPopWindowRemoveBinding.showAtLocation(rootview, Gravity.CENTER, 0, 0);
    }


    //认绑定失败 我在想想
    @Override
    public void removeBindingIThink() {
        mPopWindowRemoveBinding.dismiss();
    }
    //认绑定失败 确认解绑
    @Override
    public void removeBindingConfirmUnbundling() {
        mPopWindowRemoveBinding.dismiss();
    }
    //使用说明
    @Override
    public void toDirectionsUsePager() {
        Intent intent = new Intent(FirstChangeAddDevicesActvity.this, FirstChangeDirectionsUseActvity.class);
        intent.putExtra("identification_code",0);
        startActivity(intent);
    }

    @Override
    public void finshPager() {
        finish();
    }


    // 查找输入验证码的控件
    private void findInputBandingCodeView(View contentView) {
        tv_title_decs = (TextView)contentView.findViewById(R.id.first_change_add_devices_input_banding_code_tv_title_decs);
        et_input_banding_code1 = (EditText)contentView.findViewById(R.id.first_change_add_devices_input_banding_code_et_input_banding_code);
        tv_input_banding_code_cancel = (TextView)contentView.findViewById(R.id.first_change_add_devices_input_banding_code_tv_input_banding_code_cancel);
        tv_banding_code_confirm = (TextView)contentView.findViewById(R.id.first_change_add_devices_input_banding_code_tv_banding_code_confirm);

        SpannableString ss = new SpannableString("请在胎心仪背部标签查看绑定码");//定义hint的值
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(14,true);//设置字体大小 true表示单位是sp
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        et_input_banding_code1.setHint(new SpannedString(ss));

        tv_input_banding_code_cancel.setOnClickListener(this);
        tv_banding_code_confirm.setOnClickListener(this);

    }
    //查找绑定失败的控件
    private void findBindingFailure(View contentView) {
        tv_desc = (TextView)contentView.findViewById(R.id.first_change_add_devices_input_banding_code_tv_desc);
        tv_error_content = (TextView)contentView.findViewById(R.id.first_change_add_devices_input_banding_code_tv_error_content);
        tv_prompt = (TextView)contentView.findViewById(R.id.first_change_add_devices_input_banding_code_tv_prompt);
        tv_end_binding = (TextView)contentView.findViewById(R.id.first_change_add_devices_input_banding_code_tv_end_binding);
        tv_rebind = (TextView)contentView.findViewById(R.id.first_change_add_devices_input_banding_code_tv_rebind);
        tv_end_binding.setOnClickListener(this);
        tv_rebind.setOnClickListener(this);
    }

    //查找确认绑定失败的控件
    private void findRemoveBinding(View contentView) {
        tv_title = (TextView)contentView.findViewById(R.id.first_change_add_devices_remove_banding_tv_title);
        tv_prompt_desc = (TextView)contentView.findViewById(R.id.first_change_add_devices_remove_banding_tv_prompt_desc);
        tv_i_think = (TextView)contentView.findViewById(R.id.first_change_add_devices_remove_banding_tv_i_think);
        tv_confirm_unbundling = (TextView)contentView.findViewById(R.id.first_change_add_devices_remove_banding_tv_confirm_unbundling);

        tv_i_think.setOnClickListener(this);
        tv_confirm_unbundling.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.activity_first_change_add_devices_iv_cross:
                mFirstChangeAddDevicesPersenter.onClickeCross();
                break;
            case R.id.activity_first_change_add_devices_bt_next_stop:
                mFirstChangeAddDevicesPersenter.onClickBtNext();
                break;
            case R.id.first_change_add_devices_input_banding_code_tv_input_banding_code_cancel:  //输入绑定的码 取消
                mFirstChangeAddDevicesPersenter.cancelInputBandingCode();
                break;
            case R.id.first_change_add_devices_input_banding_code_tv_banding_code_confirm:  //输入绑定的  确认绑定
                mFirstChangeAddDevicesPersenter.inputBandingCodeConfirmBinding(this,devices_id);
                break;
            case R.id.first_change_add_devices_input_banding_code_tv_end_binding:    //绑定失败 结束绑定
                mFirstChangeAddDevicesPersenter.bindingFailureEndBinding(this,devices_id);
                break;
            case R.id.first_change_add_devices_input_banding_code_tv_rebind:   //绑定失败 重新绑定
                mFirstChangeAddDevicesPersenter.bindingFailureRebind(this,devices_id);
                break;
            case R.id.first_change_add_devices_remove_banding_tv_i_think:    //认绑定失败 我在想想
                mFirstChangeAddDevicesPersenter.verifyBindingFailedIThink();
                break;
            case R.id.first_change_add_devices_remove_banding_tv_confirm_unbundling:  //认绑定失败 确认解绑
                mFirstChangeAddDevicesPersenter.verifyBindingFailedConfirmUnbundling();
                break;
            case R.id.activity_first_change_add_devices_tv_method_use: //
                mFirstChangeAddDevicesPersenter.onClickDirectionsUse();
                break;
        }
    }


    // TODO 获取焦点
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);

    }
    // TODO 失去焦点  可以进行一些轻量级的存储数据和去初始化工作
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);

    }
}
